package com.semptian.security.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.semptian.security.service.MyAuthenticationFailureHandler;
import com.semptian.security.service.MyAuthenticationFilter;
import com.semptian.security.service.MyAuthenticationSuccessHandler;
import com.semptian.security.service.UnauthorizedEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;

import java.util.Properties;

/**
 * @author zww
 * @date 2018-04-28 09:24
 * <p>
 * Email 971762302@qq.com
 * <p>
 * Describe:
 */
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling().authenticationEntryPoint(new UnauthorizedEntryPoint()).and()
                .csrf().disable()
                .authorizeRequests() // 请求授权
                .antMatchers("/css/**").permitAll()
                .antMatchers("/font/**").permitAll()
                .antMatchers("/images/**").permitAll()
                .antMatchers("/lay/**").permitAll()
                .antMatchers("/captcha.jpg").permitAll()
                .antMatchers("/style/**").permitAll()
                .antMatchers("/*.css").permitAll()
                .antMatchers("/*.js").permitAll()
                .and()
                .formLogin() //表单登陆
                .failureHandler(authenticationFailureHandler())
                .successHandler(authenticationSuccessHandler())
                .loginPage("/login")
                .usernameParameter("username")
                .passwordParameter("password")
                .defaultSuccessUrl("/")
                .permitAll()
                .and()
                .addFilter(myAuthenticationFilter(authenticationManager()))
                .authorizeRequests() // 请求授权
                .anyRequest()  // 任何请求
                .authenticated();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }


    @Bean
    public AuthenticationSuccessHandler authenticationSuccessHandler(){
        MyAuthenticationSuccessHandler myAuthenticationSuccessHandler = new MyAuthenticationSuccessHandler();
        return myAuthenticationSuccessHandler;
    }

    @Bean
    public AuthenticationFailureHandler authenticationFailureHandler(){
        MyAuthenticationFailureHandler myAuthenticationFailureHandler = new MyAuthenticationFailureHandler();
        return myAuthenticationFailureHandler;
    }

    @Bean
    public MyAuthenticationFilter myAuthenticationFilter(
            AuthenticationManager authenticationManager
    ){
        MyAuthenticationFilter myAuthenticationFilter = new MyAuthenticationFilter();
        myAuthenticationFilter.setAuthenticationManager(authenticationManager);
        myAuthenticationFilter.setUsernameParameter("username");
        myAuthenticationFilter.setPasswordParameter("password");
        myAuthenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler());
        myAuthenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler());
        return myAuthenticationFilter;
    }

    @Bean
    public DefaultKaptcha producer() {
        Properties properties = new Properties();
        properties.put("kaptcha.border", "no");
        properties.put("kaptcha.textproducer.font.color", "black");
        properties.put("kaptcha.textproducer.char.space", "5");
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
